<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Nagios</title>
<link rel="stylesheet" type="text/css" href="../C.css">
<script type="text/javascript" src="../jquery.js"></script><script type="text/javascript" src="../jquery.syntax.js"></script><script type="text/javascript" src="../yelp.js"></script>
</head>
<body id="home">
<!--<script src="https://ssl.google-analytics.com/urchin.js" type="text/javascript"></script><script type="text/javascript">
        _uacct = "UA-1018242-8";
        urchinTracker();
      </script><script>
      function englishPageVersion() {
        var href = window.location.href;
        if (href.slice(-1) == "/") {
                window.location = "index.html.en";
        } else {
                window.location = href.replace(/\.html.*/, ".html.en");
        }
         return false;
      }
      function browserPreferredLanguage() {
        var href = window.location.href;
        if (href.slice(-1) == "/") {
                window.location = href;
        } else {
                window.location = href.replace(/\.html.*/, ".html");
        }
        return false;
      }
      </script>--><div id="container">
<div id="container-inner">
<div id="mothership"><ul>
<li><a href="https://partners.ubuntu.com">Partners</a></li>
<li><a href="https://www.ubuntu.com/support/community-support">Support</a></li>
<li><a href="https://community.ubuntu.com">Community</a></li>
<li><a href="https://www.ubuntu.com">Ubuntu.com</a></li>
</ul></div>
<div id="header">
<h1 id="ubuntu-header"><a href="https://help.ubuntu.com/">Ubuntu Documentation</a></h1>
<ul id="main-menu">
<li><a class="main-menu-item current" href="https://help.ubuntu.com/">Official Documentation</a></li>
<li><a href="https://help.ubuntu.com/community/CommunityHelpWiki">Community Help Wiki</a></li>
<li><a href="https://community.ubuntu.com/t/contribute/26">Contribute</a></li>
</ul>
</div>
<div id="menu-search"><div id="search-box">
<noscript><form action="https://www.google.com/cse" id="cse-search-box"><div>
<input type="hidden" name="cx" value="003883529982892832976:e2vwumte3fq"><input type="hidden" name="ie" value="UTF-8"><input type="text" name="q" size="21"><input type="submit" name="sa" value="Search">
</div></form></noscript><!--
<script>
                document.write('<form action="https://help.ubuntu.com/search.html" id="cse-search-box">');
                document.write('  <div>');
                document.write('    <input type="hidden" name="cof" value="FORID:9">');
                document.write('    <input type="hidden" name="cx" value="003883529982892832976:e2vwumte3fq">');
                document.write('    <input type="hidden" name="ie" value="UTF-8">');
                document.write('    <input type="text" name="q" size="21">');
                document.write('    <input type="submit" name="sa" value="Search">');
                document.write('  </div>');
                document.write('</form>');
              </script>-->
</div></div>
<div class="trails"><div class="trail">
<a href="https://help.ubuntu.com/18.04" class="trail">Ubuntu 18.04</a> » <a class="trail" href="../index.html" title="Ubuntu Server Guide">Ubuntu Server Guide</a> » <a class="trail" href="monitoring.html" title="Monitoring">Monitoring</a> » </div></div>
<div id="cwt-content" class="clearfix content-area"><div id="page">
<div id="content">
<div class="links nextlinks">
<a class="nextlinks-prev" href="monitoring-overview.html" title="Overview">Previous</a><a class="nextlinks-next" href="munin.html" title="Munin">Next</a>
</div>
<div class="hgroup"><h1 class="title">Nagios</h1></div>
<div class="region">
<div class="contents"></div>
<div class="links sectionlinks" role="navigation"><ul>
<li class="links"><a class="xref" href="nagios.html#nagios-installation" title="Installation">Installation</a></li>
<li class="links"><a class="xref" href="nagios.html#nagios-configuration-overview" title="Configuration Overview">Configuration Overview</a></li>
<li class="links"><a class="xref" href="nagios.html#nagios-configuration" title="Configuration">Configuration</a></li>
<li class="links"><a class="xref" href="nagios.html#nagios-references" title="References">References</a></li>
</ul></div>
<div class="sect2 sect" id="nagios-installation"><div class="inner">
<div class="hgroup"><h2 class="title">Installation</h2></div>
<div class="region"><div class="contents">
<p class="para">
      First, on <span class="em emphasis">server01</span> install the <span class="app application">nagios</span> package.  In a terminal
      enter:
      </p>
<div class="screen"><pre class="contents "><span class="cmd command">sudo apt install nagios3 nagios-nrpe-plugin</span>
</pre></div>
<p class="para">
      You will be asked to enter a password for the <span class="em emphasis">nagiosadmin</span> user.  The user's credentials are
      stored in <span class="file filename">/etc/nagios3/htpasswd.users</span>.  To change the <span class="em emphasis">nagiosadmin</span> password, or
      add additional users to the Nagios CGI scripts, use the <span class="app application">htpasswd</span> that is part of the 
      <span class="app application">apache2-utils</span> package.
      </p>
<p class="para">
      For example, to change the password for the <span class="em emphasis">nagiosadmin</span> user enter:
      </p>
<div class="screen"><pre class="contents "><span class="cmd command">sudo htpasswd /etc/nagios3/htpasswd.users nagiosadmin</span>
</pre></div>
<p class="para">
      To add a user:
      </p>
<div class="screen"><pre class="contents "><span class="cmd command">sudo htpasswd /etc/nagios3/htpasswd.users steve</span>
</pre></div>
<p class="para">
      Next, on <span class="em emphasis">server02</span> install the <span class="app application">nagios-nrpe-server</span> package.  From a terminal on 
      server02 enter:
      </p>
<div class="screen"><pre class="contents "><span class="cmd command">sudo apt install nagios-nrpe-server</span>
</pre></div>
<div class="note" title="Note"><div class="inner"><div class="region"><div class="contents">
        <p class="para">
        <span class="app application">NRPE</span> allows you to execute local checks on remote hosts.  There are other ways of accomplishing
        this through other Nagios plugins as well as other checks.  
        </p>
      </div></div></div></div>
</div></div>
</div></div>
<div class="sect2 sect" id="nagios-configuration-overview"><div class="inner">
<div class="hgroup"><h2 class="title">Configuration Overview</h2></div>
<div class="region"><div class="contents">
<p class="para">
      There are a couple of directories containing <span class="app application">Nagios</span> configuration and check files.
      </p>
<div class="list itemizedlist"><ul class="list itemizedlist">
<li class="list itemizedlist">
          <p class="para">
          <span class="file filename">/etc/nagios3</span>: contains configuration files for the operation of the <span class="app application">nagios</span>
          daemon, CGI files, hosts, etc.
          </p>
        </li>
<li class="list itemizedlist">
          <p class="para">
          <span class="file filename">/etc/nagios-plugins</span>: houses configuration files for the service checks. 
          </p>
        </li>
<li class="list itemizedlist">
          <p class="para">
          <span class="file filename">/etc/nagios</span>: on the remote host contains the <span class="app application">nagios-nrpe-server</span> configuration files. 
          </p>
        </li>
<li class="list itemizedlist">
          <p class="para">
          <span class="file filename">/usr/lib/nagios/plugins/</span>: where the check binaries are stored.  To see the options of a check use the
          <span class="em emphasis">-h</span> option.  
          </p>
          
          <p class="para">   
          For example: <span class="cmd command">/usr/lib/nagios/plugins/check_dhcp -h</span>
          </p>
        </li>
</ul></div>
<p class="para">
      There are a plethora of checks <span class="app application">Nagios</span> can be configured to execute for any given host.  For this example Nagios
      will be configured to check disk space, DNS, and a MySQL hostgroup.  The DNS check will be on <span class="em emphasis">server02</span>, and the 
      MySQL hostgroup will include both <span class="em emphasis">server01</span> and <span class="em emphasis">server02</span>.
      </p>
<div class="note" title="Note"><div class="inner"><div class="region"><div class="contents">
        <p class="para">
        See <a class="xref" href="httpd.html" title="HTTPD - Apache2 Web Server">HTTPD - Apache2 Web Server</a> for details on setting up Apache, <a class="xref" href="dns.html" title="Domain Name Service (DNS)">Domain Name Service (DNS)</a> for DNS, and <a class="xref" href="mysql.html" title="MySQL">MySQL</a> for MySQL.
        </p>
      </div></div></div></div>
<p class="para">
      Additionally, there are some terms that once explained will hopefully make understanding Nagios configuration easier:
      </p>
<div class="list itemizedlist"><ul class="list itemizedlist">
<li class="list itemizedlist">
          <p class="para">
          <span class="em emphasis">Host</span>: a server, workstation, network device, etc that is being monitored.
          </p>
        </li>
<li class="list itemizedlist">
          <p class="para">
          <span class="em emphasis">Host Group</span>: a group of similar hosts.  For example, you could group all web servers, file server, etc.
          </p>
        </li>
<li class="list itemizedlist">
          <p class="para">
          <span class="em emphasis">Service</span>: the service being monitored on the host.  Such as HTTP, DNS, NFS, etc.
          </p>
        </li>
<li class="list itemizedlist">
          <p class="para">
          <span class="em emphasis">Service Group</span>: allows you to group multiple services together.  This is useful for grouping multiple HTTP
          for example.
          </p>
        </li>
<li class="list itemizedlist">
          <p class="para">
          <span class="em emphasis">Contact</span>: person to be notified when an event takes place.  Nagios can be configured to send emails, SMS messages, etc.
          </p>
        </li>
</ul></div>
<p class="para">
      By default Nagios is configured to check HTTP, disk space, SSH, current users, processes, and load on the <span class="em emphasis">localhost</span>.
      Nagios will also <span class="app application">ping</span> check the <span class="em emphasis">gateway</span>.
      </p>
<p class="para">
      Large Nagios installations can be quite complex to configure.  It is usually best to start small, one or two hosts, get things configured the way you like
      then expand.
      </p>
</div></div>
</div></div>
<div class="sect2 sect" id="nagios-configuration"><div class="inner">
<div class="hgroup"><h2 class="title">Configuration</h2></div>
<div class="region"><div class="contents">
<div class="list itemizedlist"><ul class="list itemizedlist">
<li class="list itemizedlist">

          <div class="steps"><div class="inner"><ol class="steps">
<li class="steps">
              <p class="para">
              First, create a <span class="em emphasis">host</span> configuration file for <span class="em emphasis">server02</span>.
              Unless otherwise specified, run all these commands on <span class="em emphasis">server01</span>.
              In a terminal enter:
             </p>

<div class="screen"><pre class="contents "><span class="cmd command">sudo cp /etc/nagios3/conf.d/localhost_nagios2.cfg \
/etc/nagios3/conf.d/server02.cfg</span>
</pre></div>

             <div class="note" title="Note"><div class="inner"><div class="region"><div class="contents">
               <p class="para">
               In the above and following command examples, replace <span class="em emphasis">"server01"</span>, <span class="em emphasis">"server02"</span>
               <span class="em emphasis">172.18.100.100</span>, and <span class="em emphasis">172.18.100.101</span> with the host names and IP addresses
               of your servers.
               </p>
             </div></div></div></div>
             
           </li>
<li class="steps">
             <p class="para">
             Next, edit <span class="file filename">/etc/nagios3/conf.d/server02.cfg</span>:
             </p>

<div class="code"><pre class="contents ">define host{
        use                     generic-host  ; Name of host template to use
        host_name               server02
        alias                   Server 02
        address                 172.18.100.101
}

# check DNS service.
define service {
        use                             generic-service
        host_name                       server02
        service_description             DNS
        check_command                   check_dns!172.18.100.101
}
</pre></div>

           </li>
<li class="steps">

             <p class="para">
             Restart the <span class="app application">nagios</span> daemon to enable the new configuration:
             </p>

<div class="screen"><pre class="contents "><span class="cmd command">sudo systemctl restart nagio3.service</span>
</pre></div>
           
           </li>
</ol></div></div>
       </li>
<li class="list itemizedlist">
         <div class="steps"><div class="inner"><ol class="steps">
<li class="steps">

             <p class="para">
             Now add a service definition for the MySQL check by adding the following to <span class="file filename">/etc/nagios3/conf.d/services_nagios2.cfg</span>:
             </p>

<div class="code"><pre class="contents "># check MySQL servers.
define service {
        hostgroup_name        mysql-servers
        service_description   MySQL
        check_command         check_mysql_cmdlinecred!nagios!secret!$HOSTADDRESS
        use                   generic-service
        notification_interval 0 ; set &gt; 0 if you want to be renotified
}
</pre></div>

           </li>
<li class="steps">

             <p class="para">
             A <span class="em emphasis">mysql-servers</span> hostgroup now needs to be defined.  Edit 
             <span class="file filename">/etc/nagios3/conf.d/hostgroups_nagios2.cfg</span> adding:
             </p>

<div class="code"><pre class="contents "># MySQL hostgroup.
define hostgroup {
        hostgroup_name  mysql-servers
                alias           MySQL servers
                members         localhost, server02
        }
</pre></div>

           </li>
<li class="steps">

             <p class="para">
             The Nagios check needs to authenticate to MySQL.  To add a <span class="em emphasis">nagios</span> user to MySQL enter:
             </p>

<div class="screen"><pre class="contents "><span class="cmd command">mysql -u root -p -e "create user nagios identified by 'secret';"</span>
</pre></div>

             <div class="note" title="Note"><div class="inner"><div class="region"><div class="contents">
               <p class="para">
               The <span class="em emphasis">nagios</span> user will need to be added all hosts in the <span class="em emphasis">mysql-servers</span> hostgroup.
               </p>
             </div></div></div></div>

           </li>
<li class="steps">

             <p class="para">
             Restart <span class="app application">nagios</span> to start checking the MySQL servers.
             </p>

<div class="screen"><pre class="contents "><span class="cmd command">sudo systemctl restart nagios3.service</span>
</pre></div>

           </li>
</ol></div></div>
       </li>
<li class="list itemizedlist">
         <div class="steps"><div class="inner"><ol class="steps">
<li class="steps">  

             <p class="para">
             Lastly configure NRPE to check the disk space on <span class="em emphasis">server02</span>.
             </p>
 
             <p class="para">
             On <span class="em emphasis">server01</span> add the
             service check to <span class="file filename">/etc/nagios3/conf.d/server02.cfg</span>:
             </p>

<div class="code"><pre class="contents "># NRPE disk check.
define service {
        use                     generic-service
        host_name               server02
        service_description     nrpe-disk
        check_command           check_nrpe_1arg!check_all_disks!172.18.100.101
}
</pre></div>

           </li>
<li class="steps">

             <p class="para">
             Now on <span class="em emphasis">server02</span> edit <span class="file filename">/etc/nagios/nrpe.cfg</span> changing:
             </p>

<div class="code"><pre class="contents ">allowed_hosts=172.18.100.100
</pre></div>

             <p class="para">
             And below in the command definition area add:
             </p>

<div class="code"><pre class="contents ">command[check_all_disks]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -e
</pre></div>

           </li>
<li class="steps">

             <p class="para">
             Finally, restart <span class="app application">nagios-nrpe-server</span>:
             </p>

<div class="screen"><pre class="contents "><span class="cmd command">sudo systemctl restart nagios-nrpe-server.service</span>
</pre></div>

           </li>
<li class="steps">

             <p class="para">
             Also, on <span class="em emphasis">server01</span> restart <span class="app application">nagios</span>:
             </p>

<div class="screen"><pre class="contents "><span class="cmd command">sudo systemctl restart nagios3.service</span>
</pre></div>

           </li>
</ol></div></div>
       </li>
</ul></div>
<p class="para">
      You should now be able to see the host and service checks in the Nagios CGI files.  To access them point a 
      browser to http://server01/nagios3.  You will then be prompted for the <span class="em emphasis">nagiosadmin</span> username 
      and password.
      </p>
</div></div>
</div></div>
<div class="sect2 sect" id="nagios-references"><div class="inner">
<div class="hgroup"><h2 class="title">References</h2></div>
<div class="region"><div class="contents">
<p class="para">
      This section has just scratched the surface of Nagios' features.  The <span class="app application">nagios-plugins-extra</span> and 
      <span class="app application">nagios-snmp-plugins</span> contain many more service checks.
      </p>
<div class="list itemizedlist"><ul class="list itemizedlist">
<li class="list itemizedlist">
          <p class="para">
          For more information see <a href="http://www.nagios.org/" class="ulink" title="http://www.nagios.org/">Nagios</a> website.
          </p>
        </li>
<li class="list itemizedlist">
          <p class="para">
          Specifically the <a href="http://nagios.sourceforge.net/docs/3_0/" class="ulink" title="http://nagios.sourceforge.net/docs/3_0/">Online Documentation</a> site.
          </p>
        </li>
<li class="list itemizedlist">
          <p class="para">
          There is also a list of <a href="http://www.nagios.org/propaganda/books/" class="ulink" title="http://www.nagios.org/propaganda/books/">books</a> related to Nagios
          and network monitoring:
          </p>
        </li>
<li class="list itemizedlist">
          <p class="para">
          The <a href="https://help.ubuntu.com/community/Nagios3" class="ulink" title="https://help.ubuntu.com/community/Nagios3">Nagios Ubuntu Wiki</a> page also has more details.
          </p>
        </li>
</ul></div>
</div></div>
</div></div>
</div>
<div class="links nextlinks">
<a class="nextlinks-prev" href="monitoring-overview.html" title="Overview">Previous</a><a class="nextlinks-next" href="munin.html" title="Munin">Next</a>
</div>
<div class="clear"></div>
</div>
<div id="pagebottom"></div>
</div></div>
</div>
<div id="footer"><p>The material in this document is available under a free license, see <a href="https://help.ubuntu.com/legal.html">Legal</a> for details.<br>
          For information on contributing see the <a href="https://wiki.ubuntu.com/DocumentationTeam">Ubuntu Documentation Team wiki page</a>.
          To report errors in this serverguide documentation, <a href="https://bugs.launchpad.net/serverguide">file a bug report</a>.</p></div>
</div>
</body>
</html>
